﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System.Windows;
using System.Windows.Controls;

namespace FinPlusWPFControls
{
    //Note Prime use edit template columns use primative controls for data template
    public partial class FinPlusValueCheckBox : FinPlusControl
    {
        public override string Id { get { return GetValue(IdProperty).ToString(); } set { } }
        public override string[] TestStates { get { return new string[] { "TestString", "ViewString", "IsValid" }; } }
        public override string TestString { get { return string.Format("{0}|{1}", IsEnabled, IsChecked); } }

        public string ViewString { get { return string.Format("{0}|{1}", checkBox.IsEnabled, checkBox.IsChecked);  } }
        public string FunctionName { get { return GetValue(FunctionNameProperty).ToString(); } set { } }
        public object IsChecked { get { return GetValue(IsCheckedProperty); }    set { SetValue(IsCheckedProperty, value); } }
        public new bool IsEnabled { get { return (bool)GetValue(IsEnabledProperty); } set { } }

        public static readonly DependencyProperty IdProperty = DependencyProperty.Register(Propertys.Id.ToString(), typeof(string), typeof(FinPlusValueCheckBox));
        public static readonly DependencyProperty FunctionNameProperty = DependencyProperty.Register(Propertys.FunctionName.ToString(), typeof(string), typeof(FinPlusValueCheckBox));
        public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register(Propertys.IsChecked.ToString(), typeof(object), typeof(FinPlusValueCheckBox), new UIPropertyMetadata(Callback));
        public new static readonly DependencyProperty IsEnabledProperty = DependencyProperty.Register(Propertys.IsEnabled.ToString(), typeof(bool), typeof(FinPlusValueCheckBox));

        private enum Propertys { Id, FunctionName, IsChecked, IsEnabled }
        private bool _hasSetEnable;

        //construct
        public FinPlusValueCheckBox()
        {
            InitializeComponent();
            IsValid = true;

            checkBox.Cursor = ((TextBlock)this.Resources["CHit"]).Cursor;
        }

        //public
        public void SetControl()
        {
            if (!_hasSetEnable)
            {
                checkBox.IsEnabled = IsEnabled && GetPermission(FunctionName, Propertys.Id.ToString(), Id).CanWrite;
                _hasSetEnable = true;
            }
        }

        //event
        private static void Callback(DependencyObject d, DependencyPropertyChangedEventArgs a){((FinPlusValueCheckBox)d).SetControl();}
    }
}